package com.lizemin.builderfactorycase.factory.normal;

import com.lizemin.builderfactorycase.factory.entity.FileParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @author lzm
 * @date 2025/10/19 16:00
 * @description
 */
public interface ParserFactory {

    /**
     * 解析器工厂缓存
     */
    Map<String, ParserFactory> PARSER_FACTORY_CACHE = Map.ofEntries(
            Map.entry("pdf", new PdfFileParserFactory()),
            Map.entry("doc", new DocFileParserFactory()),
            Map.entry("yaml", new YamlFileParserFactory()),
            Map.entry("json", new JsonFileParserFactory()),
            Map.entry("txt", new TextFileParserFactory())
    );

    Logger log = LoggerFactory.getLogger(ParserFactory.class);

    /**
     * 获取解析器
     *
     * @return 解析器
     */
    FileParser getInstance();

    /**
     * 获取解析器对象实例
     *
     * @param type 文件类型
     * @return 解析器对象实例
     */
    static FileParser getFileParser(String type) {
        log.info("开始从缓存中获取{}类型的解析器对象", type);
        ParserFactory parserFactory = PARSER_FACTORY_CACHE.get(type);
        if (parserFactory == null) {
            throw new RuntimeException("不支持的文件类型");
        }
        return parserFactory.getInstance();
    }

}
